﻿using System;
using System.Collections;
using System.Configuration;
using System.Globalization;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SQLExecute; 

public partial class edit_form_phep : System.Web.UI.Page
{
    public string strMessage=string.Empty;
    DataSet ds;
  
    DateTime dt1, dt2;
    CultureInfo culture;
    DateTimeStyles styles = DateTimeStyles.None;
    string email;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["username"] != null && Request.Params["id"] != null)
            {
                Init_Combo();
                Get_Data_Nghi_Phep();
                lUsername.Text = Session["username"].ToString();
                lFullName.Text = Session["fullname"].ToString();
            }
        
        }
    }
    protected void Init_Combo()
    {

        cbNghi.Items.Add("Buổi sáng");
        cbNghi.Items.Add("Buổi chiều");
        cbNghi.Items.Add("Nguyên ngày");
        cbNghi.Items.Insert(0, new ListItem("Choose One", "Choose One"));

        cbNghi.SelectedIndex = 0;       
        cbLoaiPhep.Items.Add("Phép năm");
        cbLoaiPhep.Items.Add("Phép đặc biệt");
        cbLoaiPhep.Items.Add("Nghỉ không hưởng lương");
        cbLoaiPhep.Items.Add("Nghỉ ốm");

        cbLoaiPhep.Items.Insert(0, new ListItem("Choose One", "Choose One"));
        cbLoaiPhep.SelectedIndex = 0;


    }

    protected void Get_Data_Nghi_Phep()
    {
        int id = Convert.ToInt16(Request.Params["id"].ToString());
        NghiPhep n = new NghiPhep();
        n = n.Get_Info_Nghi_Phep(id);
        //cbPhongBan.SelectedValue = n.MaPhongBan.ToString();
        lPhongBan.Text = Session["tenphongban"].ToString();        
        txtFromDate.Text = n.NghiTuNgay.ToShortDateString();
        
        txtToDate.Text = n.NghiDenNgay.ToShortDateString();
       
        
        cbNghi.Text = n.ThoiGianNghi;
        cbLoaiPhep.Text = n.LoaiPhep;
        txtLydo.Text = n.LyDo;

        
    }
    protected void lnkLogout_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        Session.Clear();
        Response.Redirect("login.aspx");
    }
    protected void btnNghiPhepCu_Click(object sender, EventArgs e)
    {
        Response.Redirect("list_nghiphep.aspx");
    }
    protected bool Check_Valid_Date(DateTime dt1, DateTime dt2)
    {
        if (dt1 > dt2)
              return false;
        if (dt1 < DateTime.Now || dt2 < DateTime.Now)
            return false;

        return true;
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (txtFromDate.Text != "" && txtToDate.Text != "")
        {
            culture = CultureInfo.CreateSpecificCulture("fr-FR");
            DateTime.TryParse(txtFromDate.Text, culture, styles, out dt1);
            DateTime.TryParse(txtToDate.Text, culture, styles, out dt2);
            
            int id = Convert.ToInt16(Request.Params["id"].ToString());
            email = Session["email"].ToString();

            NghiPhep n = new NghiPhep();

            n.Update_Nghi_Phep(id, email, dt1, dt2, cbLoaiPhep.Text, txtLydo.Text, cbNghi.Text);
            lMessage.Text = "Cập nhật thành công.";
            msgBox.ShowOnPageLoad = true;
        }
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        msgBox.ShowOnPageLoad = false;
    }
}
